package com.linkage.educloud.ah.view.activity.topic;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.adapter.topic.TopicChildListAdapter;
import com.linkage.educloud.ah.bean.LabelBean;
import com.linkage.educloud.ah.bean.TopicChildBean;
import com.linkage.educloud.ah.mvp.contract.topic.ITopicChildContract;
import com.linkage.educloud.ah.mvp.presenter.topic.TopicChildPresenter;
import com.linkage.educloud.ah.view.BasicActivity;
import com.zzhou.common.library.util.ToastShow;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Mr'Tang on 2018/12/6.
 */

public class TopicSearchActivity extends BasicActivity implements ITopicChildContract.IView {

    @BindView(R.id.common_back)
    ImageView common_back;
    @BindView(R.id.common_edittext)
    EditText common_edittext;
    @BindView(R.id.common_search)
    TextView common_search;
    @BindView(R.id.common_recylerview)
    XRecyclerView common_recylerview;

    private static final String KEY_ID = "id";
    private static final String KEY_LABEL = "label_name";
    ITopicChildContract.IPresenter iPresenter;
    TopicChildListAdapter adapter;
    List<TopicChildBean> list = new ArrayList<>();
    int page = 1;

    public static void launch(Context context, String id, String label_name) {
        Intent intent = new Intent(context, TopicSearchActivity.class);
        intent.putExtra(KEY_ID, id);
        intent.putExtra(KEY_LABEL, label_name);
        context.startActivity(intent);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_topic_search;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iPresenter = new TopicChildPresenter(this, this);
        initRecylerview();
        common_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        common_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iPresenter.getCards(getIntent().getStringExtra(KEY_ID), common_edittext.getText().toString().trim(), "1", getIntent().getStringExtra(KEY_LABEL), page);
            }
        });
    }

    private void initRecylerview() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        common_recylerview.setLayoutManager(layoutManager);
        common_recylerview.setItemAnimator(new DefaultItemAnimator());
        common_recylerview.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        common_recylerview.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);

        common_recylerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                iPresenter.getCards(getIntent().getStringExtra(KEY_ID), common_edittext.getText().toString().trim(), "1", getIntent().getStringExtra(KEY_LABEL), page);
            }

            @Override
            public void onLoadMore() {
                page++;
                iPresenter.getCards(getIntent().getStringExtra(KEY_ID), common_edittext.getText().toString().trim(),"1", getIntent().getStringExtra(KEY_LABEL), page);
            }
        });

        adapter = new TopicChildListAdapter(this, list);
        common_recylerview.setAdapter(adapter);
    }

    @Override
    public void initRecylerviewDate(List<TopicChildBean> mlist) {
        if (mlist.size() == 0) {
            ToastShow.show(this, "暂无您查询的帖子！");
            return;
        }
        if (mlist.size() < 10) {
            common_recylerview.setLoadingMoreEnabled(false);
        } else {
            common_recylerview.setLoadingMoreEnabled(true);
        }
        adapter.addAll(mlist, page != 1);
    }

    @Override
    public void stopRefresh() {
        common_recylerview.refreshComplete();
        common_recylerview.loadMoreComplete();
    }

    @Override
    public void initLabelDate(List<LabelBean> list) {

    }
}
